package com.pdstudio.carrecom.ui.activity.buy;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pdstudio.carrecom.R;
import com.pdstudio.carrecom.api.HttpExecuteJson;
import com.pdstudio.carrecom.api.HttpExecuteJson.httpReturnJson;
import com.pdstudio.carrecom.api.ServiceHelper;
import com.pdstudio.carrecom.bean.ResultInfo;
import com.pdstudio.carrecom.bean.WeatherInfo;
import com.pdstudio.carrecom.bean.WeatherList;
import com.pdstudio.carrecom.logger.Logger;
import com.pdstudio.carrecom.tools.StringUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 
 * 类: ActivityBuy <br>
 * 描述: 我要买车 <br>
 * 作者: ws  <br>
 * 时间: 2015-6-15 下午11:32:03
 */
public class ActivityBuy extends Activity implements OnClickListener {

	private static final String TAG = "ActivityBuy";

	private static final int MSG_WHAT_ABOUT = 1;

	private TextView txtTitle;
	private ImageView ivBack;

	private ResultInfo mResultInfo;

	private TextView mAboutInfo;
	
	private LinearLayout mLinearLayoutNewCar;
	private LinearLayout mLinearLayoutExchange;
	private LinearLayout mLinearLayoutBoutique;//精品展示

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_buy);

		initTitle();
		initView();
	}

	@Override
	protected void onResume() {
		super.onResume();

		//getAbountInfo();
	}

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Logger.i(TAG, msg);
			switch (msg.what) {
			case MSG_WHAT_ABOUT:// 关于
				if (mResultInfo != null) {
					mAboutInfo.setText(mResultInfo.data.toString());
				}
				break;

			default:
				break;
			}
		}
	};

	/**
	 * 方法: initTitle <br>
	 * 描述: 初始化页面标题 <br>
	 * 作者: ws <br>
	 * 时间: 2015-5-24 下午5:46:11
	 */
	private void initTitle() {
		txtTitle = (TextView) findViewById(R.id.title_text_nav);

		txtTitle.setText("我要买车");

		ivBack = (ImageView) findViewById(R.id.title_back);
		ivBack.setOnClickListener(this);
	}

	private void initView() {
		mAboutInfo = (TextView) findViewById(R.id.id_about_content);
		mLinearLayoutNewCar = (LinearLayout) findViewById(R.id.buy_newcar);
		mLinearLayoutExchange = (LinearLayout) findViewById(R.id.more_suggestion);
		mLinearLayoutBoutique = (LinearLayout) findViewById(R.id.more_modifierpass);
		
		mLinearLayoutBoutique.setOnClickListener(this);
		mLinearLayoutExchange.setOnClickListener(this);
		mLinearLayoutNewCar.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			break;
		case R.id.more_modifierpass://精品展示
			Intent boutiqueIntent = new Intent(this,ActivityBoutique.class);
			startActivity(boutiqueIntent);
			break;
		case R.id.more_suggestion://二手车交换
			Intent exchangeIntent = new Intent(this,ActivityExchange.class);
			startActivity(exchangeIntent);
			break;
		case R.id.buy_newcar://新车购买
			Intent newCarIntent = new Intent(this,ActivityNewCar.class);
			startActivity(newCarIntent);
			break;
		}
	}

	/**
	 * 方法: getAbountInfo <br>
	 * 描述: 获取关于信息 <br>
	 * 作者: ws <br>
	 * 时间: 2015-5-24 下午8:40:10
	 */
	private void getAbountInfo() {
		try {
			HttpExecuteJson http = new HttpExecuteJson(this,
					mGetAbountInfoListener);
			// TODO: 从保存城市地方拿数据
			String url = ServiceHelper.About;
			Logger.i(url);
			http.get(url, null);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private httpReturnJson mGetAbountInfoListener = new httpReturnJson() {

		@Override
		public void onSuccess(String result) {
			Logger.json(result);
			ResultInfo resultInfo = null;
			try {
				resultInfo = new Gson().fromJson(result,
						new TypeToken<ResultInfo>() {
						}.getType());
				if ((resultInfo != null)
						&& (resultInfo.msg
								.equals(ServiceHelper.msg_code_sucess))) {
					try {
						mResultInfo = resultInfo;
						Logger.i("天气", resultInfo);
						Message msg = new Message();
						msg.obj = mResultInfo;
						msg.what = MSG_WHAT_ABOUT;
						mHandler.sendMessage(msg);

					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {

				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		@Override
		public void onFailure(int error, String msg) {

		}

		@Override
		public void onCancel() {

		}
	};

}
